package com.controller;


import com.mapper.helping.IssueCommentMapper;
import com.mapper.helping.IssueCommentResponseGrandsonMapper;
import com.mapper.helping.IssueCommentResponseMapper;
import com.mapper.helping.IssueMapper;
import com.mapper.living.UserDao;
import com.util.UserUtil;
import io.swagger.annotations.*;
import living.com.result.ApiResult;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <P>
 *     安全模块请求处理
 * </P>
 * @author 大忽悠
 * @create 2022/2/20 13:23
 */
@Api(tags = "安全请求处理")
@RestController
@RequiredArgsConstructor
public class SecurityController {
    private final UserDao userDao;
    private final IssueMapper issueMapper;
    private final IssueCommentMapper issueCommentMapper;
    private final IssueCommentResponseMapper issueCommentResponseMapper;
    private final IssueCommentResponseGrandsonMapper issueCommentResponseGrandsonMapper;
    private static final List<String> objectTypes= Arrays.asList("issue","comment","son_comment","grant_son_comment");
    @PostMapping("/inform")
    @ApiOperation(value = "用户举报")
    public ApiResult<?> inform(@RequestParam@ApiParam("举报目标的id,举报问题就是问题id,举报回答就是回答id") Integer objectId
            , @RequestParam@ApiParam("举报原因，长度限制50字内")String reason,
                               @RequestParam@ApiParam("举报目标的类型,问题为issue,回答为comment,子评论为son_comment,孙子评论为grant_son_comment")String objectType) {
            Assert.isTrue(reason.length() <=50,"原因太长了,请限制在50字内");
            Assert.isTrue(objectTypes.contains(objectType),"举报目标类型错误");
            //判断被举报对象是否存在
        if(objectType.equals("issue"))
        {
            Assert.notNull(issueMapper.selectById(objectId),"问题不存在");
        }else if(objectType.equals("comment"))
        {
            Assert.notNull(issueCommentMapper.selectById(objectId),"回答不存在");
        }else if(objectType.equals("son_comment"))
        {
            Assert.notNull(issueCommentResponseMapper.selectById(objectId),"儿子评论不存在");
        }else if(objectType.equals("grant_son_comment"))
        {
            Assert.notNull(issueCommentResponseGrandsonMapper.selectById(objectId),"孙子评论不存在");
        }
        userDao.inform(UserUtil.getUserId(),objectId,reason,objectType);
        return ApiResult.success();
    }
}
